package FourArithmeticOperations;

/**
 * Created by dell on 2017/10/25.
 */
public class smalltree<T> {
    BTNode<T> root;
    public smalltree(){
        root = null;
    }

    public BTNode<T> CreatTree(T []po,T[] in){
        root = new BTNode<>(po[0]);
        int i = 0;
        while (in[i]!=po[0])
            i++;

        T []Lp = (T[])new Object[i];
        T []Ln = (T[])new Object[i];
        T []Rp = (T[])new Object[in.length-i-1];
        T []Rn = (T[])new Object[in.length-i-1];
        int k=0;
        int l=0;

        for(int j=0;j<in.length;j++){
            if(j<i){
                Lp[l] = po[j+1];
                Ln[l] = in[j];
                if(l!=Lp.length-1)
                    l++;
            }else if(j>i){
                Rp[k] = po[j];
                Rn[k] = in[j];
                k++;
            }
        }
        if(i>0) {
            root.left = CreatTree(Lp, Ln);
            root.right=CreatTree(Rp, Rn);
        }else {
            root.left=null;
            root.right=null;
        }
        return root;
    }
}
